
#pragma once


namespace WTL
{
template <bool t_bManaged>
class CStgMediumT 
{
public:
		// Data members
		STGMEDIUM m_sHandle;		 
	  CStgMediumT()
	  {
				ZeroMemory(&m_sHandle, sizeof(STGMEDIUM));
	  }

		CStgMediumT(STGMEDIUM handle)
	  {
				ZeroMemory(&m_sHandle, sizeof(STGMEDIUM));
				Attach(handle);
	  }
	  ~CStgMediumT()
	  {
				if (t_bManaged)
				{
						Release();
				}	          
	  }

		void Release()
		{
				::ReleaseStgMedium(&m_sHandle);
				ZeroMemory(&m_sHandle, sizeof(STGMEDIUM));
				
		}

		void Attach(STGMEDIUM handle)
		{
				if (t_bManaged)
				{
						Release();
				}
				SwapMemory(&handle, &m_sHandle, sizeof(STGMEDIUM));
		}

	  STGMEDIUM Detach() throw()
	  {
	      m_sHandle = NULL;     
				return m_sHandle;
	  }

		 STGMEDIUM* operator->() { return &m_sHandle;}
		 STGMEDIUM* operator&() { return &m_sHandle; }

};
typedef CStgMediumT<true> CStgMedium;
typedef CStgMediumT<false> CStgMediumHandle;
}